/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Xml;

import java.io.File;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import Functions.Messages;
import javax.xml.transform.TransformerConfigurationException;

/**
 *
 * @author Saarli
 */
public class XmlWriter 
{
    DocumentBuilderFactory Factory;
    DocumentBuilder Builder;
    Document Doc;
    TransformerFactory TransFactory;
    Transformer Transform; 
    DOMSource Source;
    StreamResult Output;
    
    public XmlWriter() throws ParserConfigurationException, TransformerConfigurationException
    {
        
            Factory=DocumentBuilderFactory.newInstance();
            Builder= Factory.newDocumentBuilder();
            
            TransFactory=TransformerFactory.newInstance();
            Transform=TransFactory.newTransformer();
            
    }
    //folgende Methode muss wahrscheinlich später nochmal überarbeitet werden
    public void writeUserMessages(List<String> players) throws ParserConfigurationException, TransformerConfigurationException, TransformerException  
    {
       Doc=Builder.newDocument();
       
       Element rootElement=Doc.createElement("Messages");
       Doc.appendChild(rootElement);  
       
       for(int i=0;i<players.size();i++)
       {
           
           Element player= Doc.createElement("player");
           player.appendChild(Doc.createTextNode(players.get(i)));
           
           //Methode um empfangene Nachrichten  wenn möglich als stringlist perPlayerName zu erhalten,diese an Messasge anhängen...
           Element Message=Doc.createElement("Message");
           
           player.appendChild(Message);
           
          rootElement.appendChild(player);
       }
       
       Source = new DOMSource(Doc);
       Output=new StreamResult("./UserMessages.xml");
       
       Transform.transform(Source, Output);
    }
    public void writeServerMessages(List<String> messages) throws ParserConfigurationException, TransformerConfigurationException, TransformerException
    {
       Doc=Builder.newDocument();
       
       Element rootElement=Doc.createElement("ServerMessages");
       Doc.appendChild(rootElement);  
       
       Element welcomeMessage=Doc.createElement("welcomeMessage");
       welcomeMessage.appendChild(Doc.createTextNode(""));//Insert function to get Welcome message
       
       Element otherMessages=Doc.createElement("othermessages");
        for(int i=0;i<messages.size();i++)
       {
           otherMessages.appendChild(Doc.createElement("message").appendChild(Doc.createTextNode(messages.get(i))));
       }
       rootElement.appendChild(otherMessages);
       
       
       Source = new DOMSource(Doc);
       Output=new StreamResult("./ServerMessages.xml");
       Transform.transform(Source, Output);
    }

}
    

